package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘柄华
 * Date: 2022-06-08
 * Time: 13:15
 */
public class UserDao {
    // 通过这个类来封装对于 User 表的操作
    // 1. 实现新增用户. 用来实现注册功能.
    // 2. 根据用户名来查找到用户对象. (这个是登录的时候必要操作)
    // 3. 根据用户 id 来查找用户对象. (这个是后续在博客页面上显示作者的时候, 必要的方法)
    public void insert(User user){
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();

            String sql = "insert into user values (null, ?, ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getUsername());
            statement.setString(2,user.getPassword());

            int ret = statement.executeUpdate();
            if(ret == 1){
                System.out.println("插入用户成功");
            }else {
                System.out.println("插入用户失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }


    public User selectByName(String username){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();

            String sql = "select * from user where username= ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,username);

            resultSet = statement.executeQuery();

            if(resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }

    public User selectById(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtil.getConnection();

            String sql = "select * from user where userId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,userId);

            resultSet = statement.executeQuery();

            if(resultSet.next()){
                User user = new User();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }


    public static void main(String[] args) {
        // 测试
        UserDao userDao = new UserDao();

//        // 1.测试新增
//        User user = new User();
//        user.setUsername("张三");
//        user.setPassword("123");
//        userDao.insert(user);

//        User user = userDao.selectByName("张三");
//        System.out.println(user);
    }
}
